package com.xmall.member.handler;

import cn.hutool.core.collection.CollectionUtil;
import com.rabbitmq.client.Channel;
import com.xmall.config.AmqpCommonConfig;
import com.xmall.msg.queue.MemberModule;
import com.xmall.msg.entity.order.OrderCreateMsg;
import com.xmall.member.service.ICartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 删除购物车
 */
@Slf4j
@Component
@RabbitListener(queues = MemberModule.OrderCreate.ORDER_CREATE_QUEUE, containerFactory = AmqpCommonConfig.RABBIT_LISTENER_CONTAINER_FACTORY_NAME)
public class OrderCreateHandler {

    @Resource
    private ICartService cartService;

    @RabbitHandler
    public void msg(Channel channel, Message message, OrderCreateMsg msg) throws IOException {
        log.info("order: OrderCreateHandler");
        try {
            if(CollectionUtil.isNotEmpty(msg.getCartIds())) {
                cartService.deleteCart(msg.getCartIds());
            }
        }catch (Exception e) {}
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
